home *** CD-ROM | disk | FTP | other *** search
- Path: crl.crl.com!not-for-mail
- From: bobfry@crl.com (Robert Fry)
- Newsgroups: comp.lang.c
- Subject: Re: What is byte-alignment?
- Date: 1 Apr 1996 16:32:12 -0800
- Organization: CRL Dialup Internet Access
- Message-ID: <4jpsic$gri@crl.crl.com>
- References: <4jprfe$c6q@alcor.usc.edu>
- NNTP-Posting-Host: crl.com
-
- wawda@alcor.usc.edu (Abu Wawda) writes:
-
- >I often hear the phrase "byte-aligment" in this newsgroups, yet have
- >no clue what it is. Can someone please explain it to me? It comes up
- >often when people talk about structures. Thanks in advance,
-
- It refers to the requirements certain machines place on access of data
- from memory. (And I'm sure it shows up in other conditions, but this is
- the most obvious case). As an example, many RISC CPUs can read four bytes
- of data from RAM in a single bus cycle -- but only if the first byte read
- has a physical address that's aligned on a four-byte boundary (i.e. the
- last two bits of the address are zero).
-
- Imagine the following structure:
-
- struct {
- char byte;
- short word;
- long longint;
- } foo;
-
- Because of the above-mentioned reasons, many compilers will put the first
- byte of foo (foo.byte) at a doubleword boundary. (e.g. &foo == 0x20).
- Given that, C does not guarantee that &foo.word == 0x21. It might well be
- put at 0x24, with foo.longint at 0x28. Other compilers will put byte,
- word, and longint at 0x20, 0x21, and 0x23. You can't guarantee what byte
- these structures are aligned at, and you might have troubles with trying
- to guess where they are when reading, say, a binary data stream. This
- problem is the most common case of a byte-alignemnt problem that I've seen.
-
- (Note that the addresses given above are just for examples. Addresses
- won't necessarily look like integers.)
-
- Bob
-